
//
//  LZForgetPwdViewController.m
//  DoctorFAQs
//
//  Created by langZai on 2017/2/15.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZForgetPwdViewController.h"

@interface LZForgetPwdViewController ()
/** 电话号码textField */
@property (nonatomic , strong) UITextField *phoneTextField;
/** 验证码 */
@property (nonatomic , strong) UITextField *safeCodeTextField;
/** 获取Btn */
@property (nonatomic , strong) UIButton *getBtn;
/** 密码TextField */
@property (nonatomic , strong) UITextField *pwdTextField;
/** 再次填写密码TextField */
@property (nonatomic , strong) UITextField *conPwdTextField;

/** 注册界面的下面的注册按钮 */
@property (nonatomic , strong) UIButton *resetButton;
@end

@implementation LZForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"忘记密码";
    self.view.backgroundColor = KHColorFromRGB(0xffffff);
    [self createUI];
}

- (void)createUI{
    
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.text = @"+86";
    numLabel.font = FONT(16);
    numLabel.textColor = KHColorFromRGB(0x333333);
    numLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20 *xsIZE);
        make.top.equalTo(self.view).offset(65 *ysIZE);
    }];
    
    // 手机号textField
    _phoneTextField = [[UITextField alloc] init];
    //_phoneTextField.delegate = self;
    _phoneTextField.font = NEWFONT(16);
    _phoneTextField.placeholder = @"您的手机号码";
    _phoneTextField.textColor = KHColorFromRGB(0x333333);
    _phoneTextField.textAlignment = NSTextAlignmentLeft;
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    //_phoneTextView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_phoneTextField];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake((SCREEN_Width - 200) * xsIZE, 30 *ysIZE));
        make.left.equalTo(self.view.mas_left).offset(100*xsIZE);
        make.centerY.equalTo(numLabel);
        
    }];
    
    
    // 横线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = KHColorFromRGB(0x4bb92c);
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(20*xsIZE);
        make.right.equalTo(self.view).offset(-20*xsIZE);
        make.top.equalTo(self.view).offset(100 *ysIZE);
        make.height.offset(ysIZE);
    }];
    
    // 验证码
    UILabel *safeCodeLabel = [[UILabel alloc] init];
    safeCodeLabel.text = @"验证码";
    safeCodeLabel.font = FONT(16);
    safeCodeLabel.textColor = KHColorFromRGB(0x333333);
    safeCodeLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:safeCodeLabel];
    [safeCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20 * xsIZE);
        make.top.equalTo(lineView.mas_bottom).offset(20 *ysIZE);
    }];
    
    // 验证码textField
    _safeCodeTextField = [[UITextField alloc] init];
    _safeCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    _safeCodeTextField.font = NEWFONT(16);
    _safeCodeTextField.placeholder = @"填写验证码";
    _safeCodeTextField.textColor = KHColorFromRGB(0x333333);
    _safeCodeTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_safeCodeTextField];
    [_safeCodeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake((SCREEN_Width - 200) *xsIZE, 30 *ysIZE));
        make.left.equalTo(self.view.mas_left).offset(100*xsIZE);
        make.centerY.equalTo(safeCodeLabel);
    }];
    
    // 下面的横线
    UIView *lineViewB = [[UIView alloc] init];
    lineViewB.backgroundColor = KHColorFromRGB(0x4bb92c);
    [self.view addSubview:lineViewB];
    [lineViewB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).offset(20*xsIZE);
        make.right.equalTo(self.view).offset(-20*xsIZE);
        make.height.offset(ysIZE);
        make.top.equalTo(lineView.mas_bottom).offset(55 *ysIZE);
        
    }];
    
    
    // 获取button
    _getBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_getBtn setTitle:@"获取" forState:UIControlStateNormal];
    [_getBtn.layer setMasksToBounds:YES];
    [_getBtn.layer setCornerRadius:12.0];
    [_getBtn.layer setBorderWidth:1.0];
    _getBtn.layer.borderColor = KHGreenColor.CGColor;
    [_getBtn setTitleColor:KHGreenColor forState:UIControlStateNormal];
    [_getBtn setBackgroundColor:KHColorFromRGB(0xffffff)];
    [self.view addSubview:_getBtn];
    [_getBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(60*xsIZE, 25*ysIZE));
        make.right.equalTo(self.view.mas_right).offset(-20 *xsIZE);
        make.centerY.equalTo(_safeCodeTextField);
    }];
    [_getBtn addTarget:self action:@selector(getBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //密码Label
    UILabel *pwdLabel = [UILabel new];
    pwdLabel.text = @"密码";
    pwdLabel.font = FONT(16);
    pwdLabel.textColor = KHColorFromRGB(0x333333);
    pwdLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:pwdLabel];
    [pwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20 * xsIZE);
        make.top.equalTo(lineViewB.mas_bottom).offset(20 *ysIZE);
    }];
    
    // 密码textField
    _pwdTextField = [[UITextField alloc] init];
    _pwdTextField.keyboardType = UIKeyboardTypeURL;
    _pwdTextField.font = NEWFONT(16);
    _pwdTextField.placeholder = @"填写密码";
    _pwdTextField.textColor = KHColorFromRGB(0x333333);
    _pwdTextField.textAlignment = NSTextAlignmentLeft;
    _pwdTextField.secureTextEntry = YES;
    [self.view addSubview:_pwdTextField];
    [_pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake((SCREEN_Width - 200) *xsIZE, 30 *ysIZE));
        make.left.equalTo(self.view.mas_left).offset(100*xsIZE);
        make.centerY.equalTo(pwdLabel);
    }];
    
    // 密码下面的横线
    UIView *lineViewC = [[UIView alloc] init];
    lineViewC.backgroundColor = KHColorFromRGB(0x4bb92c);
    [self.view addSubview:lineViewC];
    [lineViewC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20*xsIZE);
        make.right.equalTo(self.view).offset(-20*xsIZE);
        make.height.offset(ysIZE);
        make.top.equalTo(lineViewB.mas_bottom).offset(55 *ysIZE);
        
    }];
    
    //确认密码
    UILabel *conPwdLabel = [UILabel new];
    conPwdLabel.text = @"确认密码";
    conPwdLabel.font = FONT(16);
    conPwdLabel.textColor = KHColorFromRGB(0x333333);
    conPwdLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:conPwdLabel];
    [conPwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20 * xsIZE);
        make.top.equalTo(lineViewC.mas_bottom).offset(20 *ysIZE);
    }];
    
    //再次填写密码TextField
    _conPwdTextField = [[UITextField alloc] init];
    _conPwdTextField.keyboardType = UIKeyboardTypeURL;
    _conPwdTextField.font = NEWFONT(16);
    _conPwdTextField.placeholder = @"再次填写密码";
    _conPwdTextField.textColor = KHColorFromRGB(0x333333);
    _conPwdTextField.textAlignment = NSTextAlignmentLeft;
    _conPwdTextField.secureTextEntry = YES;
    [self.view addSubview:_conPwdTextField];
    [_conPwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake((SCREEN_Width - 200) *xsIZE, 30 *ysIZE));
        make.left.equalTo(self.view.mas_left).offset(100*xsIZE);
        make.centerY.equalTo(conPwdLabel);
    }];
    
    // 再次填写密码下面的横线
    UIView *lineViewD = [[UIView alloc] init];
    lineViewD.backgroundColor = KHColorFromRGB(0x4bb92c);
    [self.view addSubview:lineViewD];
    [lineViewD mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20*xsIZE);
        make.right.equalTo(self.view).offset(-20*xsIZE);
        make.height.offset(ysIZE);
        make.top.equalTo(lineViewC.mas_bottom).offset(55 *ysIZE);
    }];
    
    // 重置密码按钮
    _resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_resetButton setTitle:@"重置密码" forState:UIControlStateNormal];
    [_resetButton setBackgroundColor:KHColorFromRGB(0x7bd661)];
    _resetButton.titleLabel.font = NEWFONT(18);
    _resetButton.layer.cornerRadius = 5;
    [_resetButton setTitleColor:KHColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [self.view addSubview:_resetButton];
    [_resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20*xsIZE);
        make.right.equalTo(self.view).offset(-20*ysIZE);
        make.top.equalTo(lineViewD.mas_bottom).offset(25 *ysIZE);
        make.height.offset(45*ysIZE);
    }];
    [_resetButton addTarget:self action:@selector(resetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}

// 重置密码按钮点击
- (void)resetBtnClick {
    if (!Predicate(PhoneRegex,_phoneTextField.text)) {
        [YJProgressHUD showMessage:@"请输入正确的手机号码" inView:self.view];
        return;
    }
    if (_safeCodeTextField.text == 0) {
        [YJProgressHUD showMessage:@"验证码不可以为空" inView:self.view];
    }
    
    if (!Predicate(PasswordRegex,_pwdTextField.text)) {
        [YJProgressHUD showMessage:@"密码格式不正确" inView:self.view];
        return;
    }
    if (!Predicate(PasswordRegex,_conPwdTextField.text)) {
        [YJProgressHUD showMessage:@"密码格式不正确" inView:self.view];
        return;
    }
    if (![_pwdTextField.text isEqualToString:_conPwdTextField.text]) {
        [YJProgressHUD showMessage:@"两次密码输入不一致" inView:self.view];
        return;
    }
    
    NSDictionary *paramas = @{@"tel":_phoneTextField.text,@"password":_pwdTextField.text,@"rand":_safeCodeTextField.text};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Login/forgetPwd" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 1) {
            
            [YJProgressHUD showMessage:@"修改成功" inView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"修改失败" inView:self.view];
    }];
    
}



// 获取验证码按钮点击
- (void)getBtnClick {
    
    [YJProgressHUD showProgress:@"" inView:self.view];
    if (!Predicate(PhoneRegex,_phoneTextField.text)) {
        [YJProgressHUD showMessage:@"请输入正确的手机号码" inView:self.view];
        return;
    }
    NSDictionary *paramas = @{@"type":@"2",@"tel":_phoneTextField.text};
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:paramas paramarsSite:@"Login/gainCode" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 3) {
            [YJProgressHUD showMessage:@"该手机号未注册" inView:self.view];
            return;
        }
        if ([dict[@"code"] intValue] == 0) {
            [YJProgressHUD showMessage:@"获取验证码失败" inView:self.view];
            return;
        }
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"" inView:self.view];
            //开始倒计时
            [self countDown];
        }
        
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"获取失败" inView:self.view];
    }];
    
}


// 倒计时按钮
- (void)countDown {
    __block NSInteger time = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮的样式
                [self.getBtn setTitle:@"获取" forState:UIControlStateNormal];
                [self.getBtn setTitleColor:KHGreenColor forState:UIControlStateNormal];
                self.getBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [self.getBtn setTitle:[NSString stringWithFormat:@"%.2d秒", seconds] forState:UIControlStateNormal];
                [self.getBtn setTitleColor:KHGaryColor forState:UIControlStateNormal];
                self.getBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}



@end
